home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / KERNEL / TIME.C < prev   
C/C++ Source or Header  |  1999-09-17  |  12KB  |  415 lines

  1. /*
  2.  *  linux/kernel/time.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  *  This file contains the interface functions for the various
  7.  *  time related system calls: time, stime, gettimeofday, settimeofday,
  8.  *                   adjtime
  9.  */
  10. /*
  11.  * Modification history kernel/time.c
  12.  * 
  13.  * 1993-09-02    Philip Gladstone
  14.  *      Created file with time related functions from sched.c and adjtimex() 
  15.  * 1993-10-08    Torsten Duwe
  16.  *      adjtime interface update and CMOS clock write code
  17.  * 1995-08-13    Torsten Duwe
  18.  *      kernel PLL updated to 1994-12-13 specs (rfc-1589)
  19.  * 1999-01-16    Ulrich Windl
  20.  *    Introduced error checking for many cases in adjtimex().
  21.  *    Updated NTP code according to technical memorandum Jan '96
  22.  *    "A Kernel Model for Precision Timekeeping" by Dave Mills
  23.  *    Allow time_constant larger than MAXTC(6) for NTP v4 (MAXTC == 10)
  24.  *    (Even though the technical memorandum forbids it)
  25.  */
  26.  
  27. #include <linux/mm.h>
  28. #include <linux/timex.h>
  29. #include <linux/smp_lock.h>
  30.  
  31. #include <asm/uaccess.h>
  32.  
  33. /* 
  34.  * The timezone where the local system is located.  Used as a default by some
  35.  * programs who obtain this value by using gettimeofday.
  36.  */
  37. struct timezone sys_tz = { 0, 0};
  38.  
  39. static void do_normal_gettime(struct timeval * tm)
  40. {
  41.         *tm=xtime;
  42. }
  43.  
  44. void (*do_get_fast_time)(struct timeval *) = do_normal_gettime;
  45.  
  46. /*
  47.  * Generic way to access 'xtime' (the current time of day).
  48.  * This can be changed if the platform provides a more accurate (and fast!) 
  49.  * version.
  50.  */
  51.  
  52. void get_fast_time(struct timeval * t)
  53. {
  54.     do_get_fast_time(t);
  55. }
  56.  
  57. #ifndef __alpha__
  58.  
  59. /*
  60.  * sys_time() can be implemented in user-level using
  61.  * sys_gettimeofday().  Is this for backwards compatibility?  If so,
  62.  * why not move it into the appropriate arch directory (for those
  63.  * architectures that need it).
  64.  */
  65. asmlinkage int sys_time(int * tloc)
  66. {
  67.     int i;
  68.  
  69.     /* SMP: This is fairly trivial. We grab CURRENT_TIME and 
  70.        stuff it to user space. No side effects */
  71.     i = CURRENT_TIME;
  72.     if (tloc) {
  73.         if (put_user(i,tloc))
  74.             i = -EFAULT;
  75.     }
  76.     return i;
  77. }
  78.  
  79. /*
  80.  * sys_stime() can be implemented in user-level using
  81.  * sys_settimeofday().  Is this for backwards compatibility?  If so,
  82.  * why not move it into the appropriate arch directory (for those
  83.  * architectures that need it).
  84.  */
  85.  
  86. asmlinkage int sys_stime(int * tptr)
  87. {
  88.     int value;
  89.  
  90.     if (!capable(CAP_SYS_TIME))
  91.         return -EPERM;
  92.     if (get_user(value, tptr))
  93.         return -EFAULT;
  94.     cli();
  95.     xtime.tv_sec = value;
  96.     xtime.tv_usec = 0;
  97.     time_adjust = 0;    /* stop active adjtime() */
  98.     time_status |= STA_UNSYNC;
  99.     time_maxerror = NTP_PHASE_LIMIT;
  100.     time_esterror = NTP_PHASE_LIMIT;
  101.     sti();
  102.     return 0;
  103. }
  104.  
  105. #endif
  106.  
  107. asmlinkage int sys_gettimeofday(struct timeval *tv, struct timezone *tz)
  108. {
  109.     if (tv) {
  110.         struct timeval ktv;
  111.         do_gettimeofday(&ktv);
  112.         if (copy_to_user(tv, &ktv, sizeof(ktv)))
  113.             return -EFAULT;
  114.     }
  115.     if (tz) {
  116.         if (copy_to_user(tz, &sys_tz, sizeof(sys_tz)))
  117.             return -EFAULT;
  118.     }
  119.     return 0;
  120. }
  121.  
  122. /*
  123.  * Adjust the time obtained from the CMOS to be UTC time instead of
  124.  * local time.
  125.  * 
  126.  * This is ugly, but preferable to the alternatives.  Otherwise we
  127.  * would either need to write a program to do it in /etc/rc (and risk
  128.  * confusion if the program gets run more than once; it would also be 
  129.  * hard to make the program warp the clock precisely n hours)  or
  130.  * compile in the timezone information into the kernel.  Bad, bad....
  131.  *
  132.  *                              - TYT, 1992-01-01
  133.  *
  134.  * The best thing to do is to keep the CMOS clock in universal time (UTC)
  135.  * as real UNIX machines always do it. This avoids all headaches about
  136.  * daylight saving times and warping kernel clocks.
  137.  */
  138. inline static void warp_clock(void)
  139. {
  140.     cli();
  141.     xtime.tv_sec += sys_tz.tz_minuteswest * 60;
  142.     sti();
  143. }
  144.  
  145. /*
  146.  * In case for some reason the CMOS clock has not already been running
  147.  * in UTC, but in some local time: The first time we set the timezone,
  148.  * we will warp the clock so that it is ticking UTC time instead of
  149.  * local time. Presumably, if someone is setting the timezone then we
  150.  * are running in an environment where the programs understand about
  151.  * timezones. This should be done at boot time in the /etc/rc script,
  152.  * as soon as possible, so that the clock can be set right. Otherwise,
  153.  * various programs will get confused when the clock gets warped.
  154.  */
  155.  
  156. int do_sys_settimeofday(struct timeval *tv, struct timezone *tz)
  157. {
  158.     static int firsttime = 1;
  159.  
  160.     if (!capable(CAP_SYS_TIME))
  161.         return -EPERM;
  162.         
  163.     if (tz) {
  164.         /* SMP safe, global irq locking makes it work. */
  165.         sys_tz = *tz;
  166.         if (firsttime) {
  167.             firsttime = 0;
  168.             if (!tv)
  169.                 warp_clock();
  170.         }
  171.     }
  172.     if (tv)
  173.     {
  174.         /* SMP safe, again the code in arch/foo/time.c should
  175.          * globally block out interrupts when it runs.
  176.          */
  177.         do_settimeofday(tv);
  178.     }
  179.     return 0;
  180. }
  181.  
  182. asmlinkage int sys_settimeofday(struct timeval *tv, struct timezone *tz)
  183. {
  184.     struct timeval    new_tv;
  185.     struct timezone new_tz;
  186.  
  187.     if (tv) {
  188.         if (copy_from_user(&new_tv, tv, sizeof(*tv)))
  189.             return -EFAULT;
  190.     }
  191.     if (tz) {
  192.         if (copy_from_user(&new_tz, tz, sizeof(*tz)))
  193.             return -EFAULT;
  194.     }
  195.  
  196.     return do_sys_settimeofday(tv ? &new_tv : NULL, tz ? &new_tz : NULL);
  197. }
  198.  
  199. long pps_offset = 0;        /* pps time offset (us) */
  200. long pps_jitter = MAXTIME;    /* time dispersion (jitter) (us) */
  201.  
  202. long pps_freq = 0;        /* frequency offset (scaled ppm) */
  203. long pps_stabil = MAXFREQ;    /* frequency dispersion (scaled ppm) */
  204.  
  205. long pps_valid = PPS_VALID;    /* pps signal watchdog counter */
  206.  
  207. int pps_shift = PPS_SHIFT;    /* interval duration (s) (shift) */
  208.  
  209. long pps_jitcnt = 0;        /* jitter limit exceeded */
  210. long pps_calcnt = 0;        /* calibration intervals */
  211. long pps_errcnt = 0;        /* calibration errors */
  212. long pps_stbcnt = 0;        /* stability limit exceeded */
  213.  
  214. /* hook for a loadable hardpps kernel module */
  215. void (*hardpps_ptr)(struct timeval *) = (void (*)(struct timeval *))0;
  216.  
  217. /* adjtimex mainly allows reading (and writing, if superuser) of
  218.  * kernel time-keeping variables. used by xntpd.
  219.  */
  220. int do_adjtimex(struct timex *txc)
  221. {
  222.         long ltemp, mtemp, save_adjust;
  223.     int result = time_state;    /* mostly `TIME_OK' */
  224.  
  225.     /* In order to modify anything, you gotta be super-user! */
  226.     if (txc->modes && !capable(CAP_SYS_TIME))
  227.         return -EPERM;
  228.         
  229.     /* Now we validate the data before disabling interrupts */
  230.  
  231.     if (txc->modes != ADJ_OFFSET_SINGLESHOT && (txc->modes & ADJ_OFFSET))
  232.       /* adjustment Offset limited to +- .512 seconds */
  233.         if (txc->offset <= - MAXPHASE || txc->offset >= MAXPHASE )
  234.             return -EINVAL;    
  235.  
  236.     /* if the quartz is off by more than 10% something is VERY wrong ! */
  237.     if (txc->modes & ADJ_TICK)
  238.         if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ)
  239.             return -EINVAL;
  240.  
  241.     cli(); /* SMP: global cli() is enough protection. */
  242.  
  243.     /* Save for later - semantics of adjtime is to return old value */
  244.     save_adjust = time_adjust;
  245.  
  246. #if 0    /* STA_CLOCKERR is never set yet */
  247.     time_status &= ~STA_CLOCKERR;        /* reset STA_CLOCKERR */
  248. #endif
  249.     /* If there are input parameters, then process them */
  250.     if (txc->modes)
  251.     {
  252.         if (txc->modes & ADJ_STATUS)    /* only set allowed bits */
  253.         time_status =  (txc->status & ~STA_RONLY) |
  254.                   (time_status & STA_RONLY);
  255.  
  256.         if (txc->modes & ADJ_FREQUENCY) {    /* p. 22 */
  257.         if (txc->freq > MAXFREQ || txc->freq < -MAXFREQ) {
  258.             result = -EINVAL;
  259.             goto leave;
  260.         }
  261.         time_freq = txc->freq - pps_freq;
  262.         }
  263.  
  264.         if (txc->modes & ADJ_MAXERROR) {
  265.         if (txc->maxerror < 0 || txc->maxerror >= NTP_PHASE_LIMIT) {
  266.             result = -EINVAL;
  267.             goto leave;
  268.         }
  269.         time_maxerror = txc->maxerror;
  270.         }
  271.  
  272.         if (txc->modes & ADJ_ESTERROR) {
  273.         if (txc->esterror < 0 || txc->esterror >= NTP_PHASE_LIMIT) {
  274.             result = -EINVAL;
  275.             goto leave;
  276.         }
  277.         time_esterror = txc->esterror;
  278.         }
  279.  
  280.         if (txc->modes & ADJ_TIMECONST) {    /* p. 24 */
  281.         if (txc->constant < 0) {    /* NTP v4 uses values > 6 */
  282.             result = -EINVAL;
  283.             goto leave;
  284.         }
  285.         time_constant = txc->constant;
  286.         }
  287.  
  288.         if (txc->modes & ADJ_OFFSET) {    /* values checked earlier */
  289.         if (txc->modes == ADJ_OFFSET_SINGLESHOT) {
  290.             /* adjtime() is independent from ntp_adjtime() */
  291.             time_adjust = txc->offset;
  292.         }
  293.         else if ( time_status & (STA_PLL | STA_PPSTIME) ) {
  294.             ltemp = (time_status & (STA_PPSTIME | STA_PPSSIGNAL)) ==
  295.                     (STA_PPSTIME | STA_PPSSIGNAL) ?
  296.                     pps_offset : txc->offset;
  297.  
  298.             /*
  299.              * Scale the phase adjustment and
  300.              * clamp to the operating range.
  301.              */
  302.             if (ltemp > MAXPHASE)
  303.                 time_offset = MAXPHASE << SHIFT_UPDATE;
  304.             else if (ltemp < -MAXPHASE)
  305.             time_offset = -(MAXPHASE << SHIFT_UPDATE);
  306.             else
  307.                 time_offset = ltemp << SHIFT_UPDATE;
  308.  
  309.             /*
  310.              * Select whether the frequency is to be controlled
  311.              * and in which mode (PLL or FLL). Clamp to the operating
  312.              * range. Ugly multiply/divide should be replaced someday.
  313.              */
  314.  
  315.             if (time_status & STA_FREQHOLD || time_reftime == 0)
  316.                 time_reftime = xtime.tv_sec;
  317.             mtemp = xtime.tv_sec - time_reftime;
  318.             time_reftime = xtime.tv_sec;
  319.             if (time_status & STA_FLL) {
  320.                 if (mtemp >= MINSEC) {
  321.                 ltemp = (time_offset / mtemp) << (SHIFT_USEC -
  322.                                   SHIFT_UPDATE);
  323.                 if (ltemp < 0)
  324.                     time_freq -= -ltemp >> SHIFT_KH;
  325.                 else
  326.                     time_freq += ltemp >> SHIFT_KH;
  327.             } else /* calibration interval too short (p. 12) */
  328.                 result = TIME_ERROR;
  329.             } else {    /* PLL mode */
  330.                 if (mtemp < MAXSEC) {
  331.                 ltemp *= mtemp;
  332.                 if (ltemp < 0)
  333.                     time_freq -= -ltemp >> (time_constant +
  334.                             time_constant +
  335.                             SHIFT_KF - SHIFT_USEC);
  336.                 else
  337.                     time_freq += ltemp >> (time_constant +
  338.                                time_constant +
  339.                                SHIFT_KF - SHIFT_USEC);
  340.             } else /* calibration interval too long (p. 12) */
  341.                 result = TIME_ERROR;
  342.             }
  343.             if (time_freq > time_tolerance)
  344.                 time_freq = time_tolerance;
  345.             else if (time_freq < -time_tolerance)
  346.                 time_freq = -time_tolerance;
  347.         } /* STA_PLL || STA_PPSTIME */
  348.         } /* txc->modes & ADJ_OFFSET */
  349.         if (txc->modes & ADJ_TICK) {
  350.         /* if the quartz is off by more than 10% something is
  351.            VERY wrong ! */
  352.         if (txc->tick < 900000/HZ || txc->tick > 1100000/HZ) {
  353.             result = -EINVAL;
  354.             goto leave;
  355.         }
  356.         tick = txc->tick;
  357.         }
  358.     } /* txc->modes */
  359. leave:    if ((time_status & (STA_UNSYNC|STA_CLOCKERR)) != 0
  360.         || ((time_status & (STA_PPSFREQ|STA_PPSTIME)) != 0
  361.         && (time_status & STA_PPSSIGNAL) == 0)
  362.         /* p. 24, (b) */
  363.         || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
  364.         == (STA_PPSTIME|STA_PPSJITTER))
  365.         /* p. 24, (c) */
  366.         || ((time_status & STA_PPSFREQ) != 0
  367.         && (time_status & (STA_PPSWANDER|STA_PPSERROR)) != 0))
  368.         /* p. 24, (d) */
  369.         result = TIME_ERROR;
  370.     
  371.     if ((txc->modes & ADJ_OFFSET_SINGLESHOT) == ADJ_OFFSET_SINGLESHOT)
  372.         txc->offset       = save_adjust;
  373.     else {
  374.         if (time_offset < 0)
  375.         txc->offset = -(-time_offset >> SHIFT_UPDATE);
  376.         else
  377.         txc->offset = time_offset >> SHIFT_UPDATE;
  378.     }
  379.     txc->freq       = time_freq + pps_freq;
  380.     txc->maxerror       = time_maxerror;
  381.     txc->esterror       = time_esterror;
  382.     txc->status       = time_status;
  383.     txc->constant       = time_constant;
  384.     txc->precision       = time_precision;
  385.     txc->tolerance       = time_tolerance;
  386.     do_gettimeofday(&txc->time);
  387.     txc->tick       = tick;
  388.     txc->ppsfreq       = pps_freq;
  389.     txc->jitter       = pps_jitter >> PPS_AVG;
  390.     txc->shift       = pps_shift;
  391.     txc->stabil       = pps_stabil;
  392.     txc->jitcnt       = pps_jitcnt;
  393.     txc->calcnt       = pps_calcnt;
  394.     txc->errcnt       = pps_errcnt;
  395.     txc->stbcnt       = pps_stbcnt;
  396.  
  397.     sti();
  398.     return(result);
  399. }
  400.  
  401. asmlinkage int sys_adjtimex(struct timex *txc_p)
  402. {
  403.     struct timex txc;        /* Local copy of parameter */
  404.     int ret;
  405.  
  406.     /* Copy the user data space into the kernel copy
  407.      * structure. But bear in mind that the structures
  408.      * may change
  409.      */
  410.     if(copy_from_user(&txc, txc_p, sizeof(struct timex)))
  411.         return -EFAULT;
  412.     ret = do_adjtimex(&txc);
  413.     return copy_to_user(txc_p, &txc, sizeof(struct timex)) ? -EFAULT : ret;
  414. }
  415.